package xkp.lesson.springboot_shiro.controller;

import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xkp.lesson.springboot_shiro.entity.JsonData;
import xkp.lesson.springboot_shiro.entity.UserInfo;
import xkp.lesson.springboot_shiro.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @PostMapping(value = "/login")
    public JsonData<UserInfo> login(String userName, String userPwd,String tryCode, HttpServletRequest request) {
        log.info(tryCode);
        Object rightCodeObj = request.getSession().getAttribute("rightCode");
        String rightCode = rightCodeObj == null?"":rightCodeObj.toString();
        if(!rightCode.equals(tryCode)){
            return new JsonData<>(-1,"验证码错误");
        }
        //log.info(request.getSession().getAttribute("rightCode").toString());
        log.info(request.getSession().getId());
        JsonData<UserInfo> jsonData = new JsonData<>();
        jsonData.setCode(1);
        jsonData.setMsg("登录成功");
        jsonData.setData(userInfoService.login(userName,userPwd));
        return jsonData;
    }

    @RequiresPermissions("userInfo:list")
    @GetMapping(value = "/selectAll")
    public JsonData<List<UserInfo>> selectAll(){
        JsonData<List<UserInfo>> jsonData = new JsonData<>();
        jsonData.setData(userInfoService.selectAll());
        jsonData.setMsg("查询成功");
        return jsonData;
    }
}
